package com.example.orderflow.controller.flow;

import com.example.orderflow.controller.base.ABaseController;
import com.example.orderflow.entity.vo.ResponseVO;
import com.example.orderflow.service.ApprovalStageService;
import com.example.orderflow.service.ApproveHistoryService;
import com.example.orderflow.service.OrderInfoService;
import com.example.orderflow.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 审核阶段：质检
 * 审核等级：7
 */

@RestController
public class QualityTestingController extends ABaseController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    ApprovalStageService approvalStageService;

    @Autowired
    ApproveHistoryService approveHistoryService;

    @Autowired
    OrderInfoService orderInfoService;

    /**
     * 质检 通过 7
     * @param map
     * orderId 订单编号
     * approveBy 审批人
     * @return
     */
    @PostMapping("/QualityTestingPass")
    protected <T> ResponseVO orderApprovalSection2Pass(@RequestBody Map<String,String> map) {

        String orderId = map.get("orderId"); //订单号
        String approveBy = map.get("userName"); //审批人
        int levelId = 7;
        String levelIdS = "7";

        //核实订单号和审批人

        //找到下一个等级的名称
        String Nextlevelname = approvalStageService.getApprovalStageByApprovalId(levelId+1).getLevelName();

        //更改单人审核状态
        approveHistoryService.updateDefaultApproveHistoryObjectState(orderId,levelIdS,approveBy,"已通过","1");

        //更改订单审核状态
        orderInfoService.updateOrderInfoLevelName(orderId,Nextlevelname);

        //添加新的审核状态
        approveHistoryService.createDefaultApproveHistory(orderId,"8","李艳飞");
        return getSuccessResponseVO("该订单质检阶段已完成");

    }

    /**
     * 质检 退回 7
     * @param map
     * orderId 订单编号
     * approveBy 审批人
     * @return
     */
    @PostMapping("/QualityTestingReturn")
    protected <T> ResponseVO orderApprovalSection1Return(@RequestBody Map<String,String> map) {

        String orderId = map.get("orderId"); //订单号
        String approveBy = map.get("userName"); //审批人
        String workshop = map.get("workshop"); //车间
        int levelId = 7;
        String levelIdS = "7";

        //核实订单号和审批人

        //获取
        String Levelname = approvalStageService.getApprovalStageByApprovalId(levelId).getLevelName();

        //更新审核记录表该审核人审核状态为退回
        approveHistoryService.updateDefaultApproveHistory(orderId,levelIdS,approveBy,"退回");

        //更新单条审核记录，记录状态为0
        approveHistoryService.updateObjectStateAndApproveTimeByOrderIdAndLevelId(orderId,levelIdS);
        approveHistoryService.updateObjectStateAndApproveTimeByOrderIdAndLevelId(orderId,"6");

        //订单状态改为已退回
        orderInfoService.updateOrderInfoLevelName(orderId,"订单生产");

        //创建订单生产重新审核条例
        approveHistoryService.createDefaultApproveHistory(orderId,"6",workshop);

        return getSuccessResponseVO("退回订单"+orderId+"成功,订单已废弃");
    }


}
